package com.governtalk.backend.repository;

import com.governtalk.backend.entity.Question;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface QuestionRepository extends JpaRepository<Question, Long> {
    
    @Query("SELECT q FROM Question q WHERE q.categoryId = :categoryId AND q.isPublic = true")
    Page<Question> findByCategoryId(@Param("categoryId") Long categoryId, Pageable pageable);
    
    @Query("SELECT q FROM Question q WHERE q.type = :type AND q.isPublic = true")
    Page<Question> findByType(@Param("type") Question.QuestionType type, Pageable pageable);
    
    @Query("SELECT q FROM Question q WHERE q.difficulty = :difficulty AND q.isPublic = true")
    Page<Question> findByDifficulty(@Param("difficulty") Question.DifficultyLevel difficulty, Pageable pageable);
    
    @Query("SELECT q FROM Question q WHERE q.content LIKE %:keyword% OR q.explanation LIKE %:keyword%")
    Page<Question> findByContentContaining(@Param("keyword") String keyword, Pageable pageable);
    
    @Query("SELECT q FROM Question q WHERE (q.content LIKE %:keyword% OR q.explanation LIKE %:keyword%) AND q.isPublic = true")
    Page<Question> findByContentContainingAndIsPublicTrue(@Param("keyword") String keyword, Pageable pageable);
    
    @Query("SELECT q FROM Question q WHERE q.categoryId = :categoryId AND q.type = :type AND q.difficulty = :difficulty")
    Page<Question> findByCategoryAndTypeAndDifficulty(@Param("categoryId") Long categoryId, 
                                                      @Param("type") Question.QuestionType type, 
                                                      @Param("difficulty") Question.DifficultyLevel difficulty, 
                                                      Pageable pageable);
    
    @Query("SELECT q FROM Question q ORDER BY RAND()")
    List<Question> findRandomQuestions(Pageable pageable);
    
    long countByCategoryId(Long categoryId);
    
    // 练习相关查询方法
    @Query("SELECT q FROM Question q WHERE q.categoryId = :categoryId AND q.difficulty = :difficulty AND q.type = :type AND q.isPublic = true ORDER BY RAND()")
    List<Question> findByCategoryIdAndDifficultyAndTypeAndIsPublicTrue(@Param("categoryId") Long categoryId, @Param("difficulty") Question.DifficultyLevel difficulty, @Param("type") Question.QuestionType type, Pageable pageable);
    
    @Query("SELECT q FROM Question q WHERE q.categoryId = :categoryId AND q.difficulty = :difficulty AND q.isPublic = true ORDER BY RAND()")
    List<Question> findByCategoryIdAndDifficultyAndIsPublicTrue(@Param("categoryId") Long categoryId, @Param("difficulty") Question.DifficultyLevel difficulty, Pageable pageable);
    
    @Query("SELECT q FROM Question q WHERE q.categoryId = :categoryId AND q.isPublic = true ORDER BY RAND()")
    List<Question> findByCategoryIdAndIsPublicTrue(@Param("categoryId") Long categoryId, Pageable pageable);
    
    @Query("SELECT q FROM Question q WHERE q.isPublic = true ORDER BY RAND()")
    List<Question> findByIsPublicTrue(Pageable pageable);
    
    @Query("SELECT q FROM Question q WHERE q.isPublic = true ORDER BY RAND()")
    Page<Question> findByIsPublicTruePage(Pageable pageable);
    
    @Query("SELECT q FROM Question q WHERE q.categoryId = :categoryId AND q.type = :type AND q.isPublic = true ORDER BY RAND()")
    List<Question> findByCategoryIdAndTypeAndIsPublicTrue(@Param("categoryId") Long categoryId, @Param("type") Question.QuestionType type, Pageable pageable);
    
    @Query("SELECT q FROM Question q WHERE q.difficulty = :difficulty AND q.type = :type AND q.isPublic = true ORDER BY RAND()")
    List<Question> findByDifficultyAndTypeAndIsPublicTrue(@Param("difficulty") Question.DifficultyLevel difficulty, @Param("type") Question.QuestionType type, Pageable pageable);
    
    @Query("SELECT q FROM Question q WHERE q.type = :type AND q.isPublic = true ORDER BY RAND()")
    List<Question> findByTypeAndIsPublicTrue(@Param("type") Question.QuestionType type, Pageable pageable);
}
